package com.ly.mpayment.rest.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.persist.entity.UpProductCode;
import com.ly.mpayment.persist.service.upproductcode.IUpProductCodeService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import net.sf.json.JSONObject;

@Path("system/upproductcode/")
@Component
public class UpProductCodeRest extends AbsRest{

	@Autowired
	IUserService userService;

	@Autowired
	IUpProductCodeService codeService;

	@Path("addProductCode")
	@POST
	public String addProductCode(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "codeName","productCode");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			UpProductCode code = this.codeService.findByCode(json.getString("productCode"));
			if(code != null){
				return ResultMessage.getFailt(ResultMessage.productCodeexistError);
			}
			code = new UpProductCode();
			code.setCodeName(json.getString("codeName"));
			code.setProductCode(json.getString("productCode"));
			code.setCodeStatus(1);
			if(codeService.saveUpProduct(code)){
				return ResultMessage.getSucceed(null);
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}

	@Path("updateProductCode")
	@POST
	public String updateProductCode(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "codeName","productCode","proCodeId");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			UpProductCode code = this.codeService.findById(json.getString("proCodeId"));
			UpProductCode codePro = this.codeService.findByCode(json.getString("productCode"));
			if(code != null && (codePro == null || codePro.getProCodeId().equals(code.getProCodeId()))){
				code.setCodeName(json.getString("codeName"));
				code.setProductCode(json.getString("productCode"));
				if(codeService.updateUpProduct(code)){
					return ResultMessage.getSucceed(null);
				}
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}


	@Path("findCode")
	@POST
	public String findCode(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "proCodeId");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			UpProductCode code = this.codeService.findById(json.getString("proCodeId"));
			return ResultMessage.getSucceed( JsonUtils.excludes(code, new String[]{""}));
		}
		return result;
	}

	@Path("findPageCode")
	@POST
	public String findPageCode(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "productCode","codeName","start","pageSize");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			Map<String, Object> parameters = MapUtil.getMap();
			if(!StringUtils.isBlank(json.getString("productCode"))){
				parameters.put(":productCode", json.getString("productCode").concat("%"));
			}
			if(!StringUtils.isBlank(json.getString("codeName"))){
				parameters.put(":codeName", json.getString("codeName"));
			}
			return ResultMessage.getSucceed( JsonUtils.excludes(this.codeService.findPage(parameters, json.getInt("start"), json.getInt("pageSize")), new String[]{""}));
		}
		return result;
	}

	@Path("findList")
	@POST
	public String findList(){
		return ResultMessage.getSucceed( JsonUtils.jsonArrayToJson(this.codeService.findList(), new String[]{""}));
	}
}
